<?php 
$GLOBALS['Md'] = "TimeClock";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
echo heade(); 
module($BSD['DB00']);
include($GLOBALS['PAD']."Modules/TimeClock/main.obj.php");

	$sysTime = date("Y-m-d H:i:s");
		
	$Mon = 'mon'.date("w");
	
	$Sevr = split(" ",$sysTime);
	$Sev = split(":",$Sevr[1]);
	$Sev = ($Sev[0]-4).':'.$Sev[1].':'.$Sev[2];
	$Sev = $Sevr[0].' '.$Sev;
	
	function timecalc($timestart,$timeend) {
		$UnixvStart = strtotime($timestart);
		$UnixvEnd = strtotime($timeend);
		$UnixCalc = $UnixvEnd-$UnixvStart;
		$Cal = date("H:i:s",$UnixCalc-3600);
		return $Cal;
	}

	// Verif Faltas
	$QProfiles = "SELECT 
		timeclock_profiles.ref,
		timeclock_profiles.main_users_ref AS USER,
		timeclock_profiles.delay,
		timeclock_profiles.{$Mon},
		timeclock_profiles.ProfileType AS Type,
		timeclock_ttables.entrytime,
		timeclock_ttables.ref AS Tabs
			FROM timeclock_profiles
		INNER JOIN timeclock_ttables ON timeclock_ttables.ref = timeclock_profiles.{$Mon}
		WHERE TIME(timeclock_ttables.entrytime) < TIME('{$Sev}')
		AND '{$sysTime}' BETWEEN timeclock_profiles.TimeIni AND timeclock_profiles.TimeEnd";
	$Profiles = $GLOBALS['CONN']->db_array($QProfiles); 
	for ($r = 1; $r <= $Profiles[0]; $r++) { 
	
		$QOp = "SELECT ref FROM timeclock_operations WHERE 
							main_users_ref = {$Profiles[$r]['USER']}
							AND DATE(time_main) = DATE('{$sysTime}')";
							$Op = $GLOBALS['CONN']->db_array($QOp); 
							
		if (!isset($Op[1]) && $Profiles[$r]['Type'] == 1) {
		
		$CVacs = "SELECT ref FROM timeclock_vacs
							WHERE RefUser = {$Profiles[$r]['USER']}
							 AND Status = 4
							 AND TimeStart < '{$sysTime}' AND
							 TimeEnd > '{$sysTime}'";
							$Jvac = $GLOBALS['CONN']->db_query($CVacs); 
							
		if (!isset($Jvac[1])) {
							
		$QOp = "INSERT INTO timeclock_operations 
							(main_users_ref, time_main, RefTTable)
							 VALUES 
							 ({$Profiles[$r]['USER']}, '{$sysTime}', 

{$Profiles[$r]['Tabs']})";
							$GLOBALS['CONN']->db_query($QOp); 

		$QOp = "SELECT ref FROM timeclock_operations WHERE 
							main_users_ref = {$Profiles[$r]['USER']}
							AND DATE(time_main) = DATE('{$sysTime}')";
							$Op = $GLOBALS['CONN']->db_array($QOp); 
							
		$QO = "INSERT INTO timeclock_delays 
				(main_user_ref, delaylevel, delaytime, EventTime, Status, RefType, RefOperation) 
				VALUES 
				({$Profiles[$r]['USER']}, 0, 0, '{$sysTime}', 1, 2, {$Op[1]['ref']})";
				$GLOBALS['CONN']->db_query($QO);
				}
		}
	
	}
	
	if (!isset($_GET['Month'])) {$_GET['Month'] = date("m");}
	if (!isset($_GET['Year'])) {$_GET['Year'] = date("Y");}
	$Mon = 'mon'.date("w");
?>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/JS/calendar.js"></script> 
<table width="100%"  border="0" cellpadding="10">
  <tr>
    <td>
	<?php echo $_GET['Time'];?>
	  <table width="100%"  border="1" cellpadding="3" cellspacing="0">
        <tr bgcolor="#F2F2F2">
          <td width="20%" rowspan="2"><div align="center">Funcionário</div></td>
          <td width="20%" rowspan="2"><div align="center">Departamento</div></td>
          <td width="10%" rowspan="2"><div align="center">In�cio </div></td>
          <td width="10%" rowspan="2"><div align="center"></div>            <div align="center">Entrada </div></td>
          <td width="10%" rowspan="2"><div align="center">Saída </div></td>
          <td colspan="2"><div align="center">AlmoÇo</div></td>
          <td colspan="2"><div align="center">Outros</div></td>
        </tr>
        <tr bgcolor="#F2F2F2">
          <td width="10%"><div align="center">Início</div></td>
          <td width="10%"><div align="center">Fim</div></td>
		  <td width="10%"><div align="center">Atrasos</div></td>
		  <td width="10%"><div align="center">Compensa��es</div></td>
        </tr>
   <?php
  $Year = substr($_GET['Time'],0,4);
  $Month = substr($_GET['Time'],4,2);
  $Day = substr($_GET['Time'],6,2);
  $Mon = 'mon'.date("w");
 if (isset($_GET['Offices']) && $_GET['Offices'] != "") { 
			$Offices = " AND main_offices.ref = {$_GET['Offices']}";
			} else {
			$Offices = "";
			}
    $QProfiles = "SELECT main_offices.name AS office, main_users.name AS name,
						 timeclock_profiles.ref,
						 timeclock_profiles.main_users_ref,
						 timeclock_profiles.delay,
						 timeclock_profiles.{$Mon},
						 timeclock_profiles.ProfileType,
						 TIME(timeclock_ttables.entrytime) AS SchedEntry
				FROM timeclock_profiles
		INNER JOIN timeclock_ttables ON timeclock_ttables.ref = timeclock_profiles.{$Mon}
		INNER JOIN main_users ON main_users.ref = timeclock_profiles.main_users_ref
		INNER JOIN main_offices ON main_users.ref_office = main_offices.ref
		WHERE '{$_GET['Time']}' BETWEEN TimeIni AND TimeEnd
		{$Offices}
		ORDER BY office ASC, name ASC";
	$Profiles = $GLOBALS['CONN']->db_array($QProfiles); 
	for ($f = 1; $f <= $Profiles[0]; $f++) { 
	
		$line1 = '<tr bgcolor="#D3D6E4">';
		$line2 = '<td>'.$Profiles[$f]['name'].'</td>
		<td>'.$Profiles[$f]['office'].'</td>';
	   
	    $CVacs = "SELECT * FROM timeclock_vacs
		WHERE 
				TimeStart <= {$_GET['Time']} AND TimeEnd >= {$_GET['Time']}
		AND timeclock_vacs.RefUser = {$Profiles[$f]['main_users_ref']}
		AND Status > 3";
		$Vacs = $GLOBALS['CONN']->db_array($CVacs);
		
	if (isset($Vacs[1])) {
	
		echo $line1.$line2.'<td colspan=7">F�rias at� '.$Vacs[1]['TimeEnd'].'</td>';
		
	} elseif ($Profiles[$f]['ProfileType'] == 2) {
	
		echo $line1.$line2.'<td colspan="7">Isento</td>';
	
	} else {
	
    $QNavTime = "SELECT TIME(InTime) AS InTime,
						TIME(OutTime) AS OutTime,
						TIME(LunchStart) AS LunchStart,
						TIME(LunchEnd) AS LunchEnd
				FROM timeclock_operations 
	WHERE DAY(time_main) = {$Day} AND MONTH(time_main) = {$Month} AND YEAR(time_main) = {$Year} 
	AND timeclock_operations.main_users_ref = {$Profiles[$f]['main_users_ref']}
	ORDER BY time_main";
	$NavTime = $GLOBALS['CONN']->db_array($QNavTime);
	 if (isset($NavTime[1]) && $NavTime[1]['InTime'] != '') { 
 
		echo $line1.$line2.'<td>'.$Profiles[$f]['SchedEntry'].'</td>
		<td>&nbsp;'.$NavTime[1]['InTime'].'</td>
		<td>&nbsp;'.$NavTime[1]['OutTime'].'</td>
		<td>&nbsp;'.$NavTime[1]['LunchStart'].'</td>
		<td>&nbsp;'.$NavTime[1]['LunchEnd'].'</td>
		<td>&nbsp;';

	$QEvent = "SELECT * FROM timeclock_delays
	WHERE DAY(EventTime) = {$Day} AND MONTH(EventTime) = {$Month} AND YEAR(EventTime) = {$Year} 
	AND main_user_ref = {$Profiles[$f]['main_users_ref']}";
	$Event = $GLOBALS['CONN']->db_array($QEvent);
	if (isset($Event[1])) { 
			echo date("H:i:s", $Event[1]['delaytime']-3600);
			}
		echo '</td><td>&nbsp;';
		
	$QEvent = "SELECT * FROM timeclock_xtime
	WHERE DAY(Time) = {$Day} AND MONTH(Time) = {$Month} AND YEAR(Time) = {$Year} 
	AND RefUser = {$Profiles[$f]['main_users_ref']}";
	$Event = $GLOBALS['CONN']->db_array($QEvent);
	if (isset($Event[1])) { 
			echo date("H:i:s", $Event[1]['EventTime']);
			}
		echo '</td>';
	
	} else { 
         
		 $line1 = '<tr bgcolor="#F2F2F2">';
		 
		echo $line1.$line2.'<td>'.$Profiles[$f]['SchedEntry'].'</td>
		<td colspan="6">NÃo compareceu </td>';
   } 
   } 
   		echo '</tr>';
   } ?>
    </table>
	</td>
	</tr>
</table>
<script charset="utf-8" type="text/javascript">window.print();</script>